package com.sskj.fabi.presenter;

import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.ToastUtil;
import com.sskj.fabi.bean.BuySellBean;
import com.sskj.fabi.bean.OrderDetailBean;
import com.sskj.fabi.bean.PayWayItem;
import com.sskj.fabi.http.HttpConfig;
import com.sskj.fabi.ui.fragment.BuySellFragment;
import com.sskj.lib.bean.PageBean;
import com.sskj.lib.http.CallBackOption;
import com.sskj.lib.http.JsonConverter;

import java.util.List;

import io.reactivex.Flowable;


public class BuySellFragmentPresenter extends BasePresenter<BuySellFragment> {

    public Flowable<List<BuySellBean>> getData(String page, String pageSize, boolean isBuy, String pType, String minMoney) {
        return httpService.getSellBuy(page, pageSize, isBuy, pType, minMoney)
                .converter(new JsonConverter<HttpData<PageBean<BuySellBean>>>() {
                })
                .adapt(new FlowableBody<>())
                .map(pageBeanHttpData -> pageBeanHttpData.getData().getList());
    }

    public void getPositionNum() {
        httpService.getPositionNum()
                .execute(new CallBackOption<HttpData<Integer>>() {
                }
                        .loadBind(mView)
                        .execute(httpData -> mView.tryTrade(httpData.getData())));
    }

    // 获取支付方式列表
    public void requsetPayWayList() {
        httpService.requestPayWayList()
                .execute(new CallBackOption<HttpData<List<PayWayItem>>>() {
                }
                        .unLoadBind(mView)
                        .execute(httpData -> {
                            mView.updateUi(httpData.getData());
                        }));

    }

    public void order(String tpwd, String order_no, String total_num, boolean isBuy, String min, String max, String type, String totalPrice, String price) {
        httpService.order(null, tpwd, order_no, total_num, isBuy, min, max, type, totalPrice, price)
                .execute(new CallBackOption<HttpData<OrderDetailBean>>() {
                }.loadBind(mView).execute(httpData -> {
                    ToastUtil.showShort(httpData.getMsg());
                    mView.orderSuccess(httpData.getData());
                }));
    }


}
